<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>体能考核评分系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui@2.9.6/dist/css/layui.min.css">
    <style>
        .container {
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .result-card {
            background: #f8f8f8;
            border-radius: 8px;
            padding: 20px;
            margin-top: 20px;
        }
        .score-item {
            display: flex;
            justify-content: space-between;
            margin: 10px 0;
            padding: 8px 0;
            border-bottom: 1px solid #eee;
        }
        .total-score {
            font-size: 18px;
            font-weight: bold;
            color: #ff5722;
            text-align: center;
            margin: 15px 0;
        }
        .grade-excellent { color: #009688; font-weight: bold; }
        .grade-good { color: #1E9FFF; font-weight: bold; }
        .grade-pass { color: #FFB800; font-weight: bold; }
        .grade-fail { color: #FF5722; font-weight: bold; }
    </style>
</head>
<body>
    <div class="container">
        <!-- 标题 -->
        <div class="header">
            <h1>体能考核评分系统</h1>
            <p class="layui-word-aux">请输入您的个人信息和体能测试成绩，系统将自动计算得分和等级</p>
        </div>

        <!-- 性别选择 -->
        <div class="layui-card">
            <div class="layui-card-header">基本信息</div>
            <div class="layui-card-body">
                <div class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="gender" value="male" title="男生" checked>
                            <input type="radio" name="gender" value="female" title="女生">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">年龄</label>
                        <div class="layui-input-inline">
                            <input type="number" id="age" placeholder="请输入年龄" class="layui-input" min="18" max="60">
                        </div>
                        <div class="layui-form-mid layui-word-aux">岁</div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">身高</label>
                        <div class="layui-input-inline">
                            <input type="number" id="height" placeholder="请输入身高" class="layui-input" min="140" max="220">
                        </div>
                        <div class="layui-form-mid layui-word-aux">厘米</div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">体重</label>
                        <div class="layui-input-inline">
                            <input type="number" id="weight" placeholder="请输入体重" class="layui-input" min="35" max="150" step="0.1">
                        </div>
                        <div class="layui-form-mid layui-word-aux">公斤</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 体能项目输入 -->
        <div class="layui-card" style="margin-top: 20px;">
            <div class="layui-card-header">体能测试成绩</div>
            <div class="layui-card-body">
                <div class="layui-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">3000米跑</label>
                        <div class="layui-input-inline">
                            <input type="text" id="run3000" placeholder="格式：MM:SS" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">例如：12:30</div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">蛇形跑</label>
                        <div class="layui-input-inline">
                            <input type="number" id="snakeRun" placeholder="请输入时间" class="layui-input" step="0.1" min="0" max="50">
                        </div>
                        <div class="layui-form-mid layui-word-aux">秒</div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">引体向上</label>
                        <div class="layui-input-inline">
                            <input type="number" id="pullUp" placeholder="请输入次数" class="layui-input" min="0" max="100">
                        </div>
                        <div class="layui-form-mid layui-word-aux">次</div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">仰卧卷腹</label>
                        <div class="layui-input-inline">
                            <input type="number" id="sitUp" placeholder="请输入次数" class="layui-input" min="0" max="100">
                        </div>
                        <div class="layui-form-mid layui-word-aux">次</div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn layui-btn-normal" id="calculate-btn">
                                <i class="layui-icon">&#xe615;</i> 计算得分
                            </button>
                            <button type="button" class="layui-btn layui-btn-primary" id="reset-btn">
                                <i class="layui-icon">&#x1002;</i> 重置
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 结果显示 -->
        <div class="result-card" id="result-section" style="display: none;">
            <div class="layui-card">
                <div class="layui-card-header">考核结果</div>
                <div class="layui-card-body">
                    <!-- BMI信息 -->
                    <div class="layui-row">
                        <div class="layui-col-xs6">
                            <div class="score-item">
                                <span>BMI指数：</span>
                                <span id="bmi-value">--</span>
                            </div>
                        </div>
                        <div class="layui-col-xs6">
                            <div class="score-item">
                                <span>BMI等级：</span>
                                <span id="bmi-grade">--</span>
                            </div>
                        </div>
                    </div>

                    <!-- 各项得分 -->
                    <div class="score-item">
                        <span>3000米跑：</span>
                        <span><span id="score-run3000">--</span>分 (<span id="grade-run3000">--</span>)</span>
                    </div>
                    <div class="score-item">
                        <span>蛇形跑：</span>
                        <span><span id="score-snakeRun">--</span>分 (<span id="grade-snakeRun">--</span>)</span>
                    </div>
                    <div class="score-item">
                        <span>引体向上：</span>
                        <span><span id="score-pullUp">--</span>分 (<span id="grade-pullUp">--</span>)</span>
                    </div>
                    <div class="score-item">
                        <span>仰卧卷腹：</span>
                        <span><span id="score-sitUp">--</span>分 (<span id="grade-sitUp">--</span>)</span>
                    </div>

                    <!-- 总分 -->
                    <div class="total-score">
                        总分：<span id="total-score">--</span>分
                    </div>

                    <!-- 总等级 -->
                    <div style="text-align: center;">
                        总等级：<span id="total-grade" class="grade-excellent">--</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/layui@2.9.6/dist/layui.min.js"></script>
    <script>
        layui.use(['form', 'layer'], function(){
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.$;

            // 计算按钮点击事件
            $('#calculate-btn').on('click', function(){
                // 获取表单数据
                var gender = $('input[name="gender"]:checked').val();
                var age = $('#age').val();
                var height = $('#height').val();
                var weight = $('#weight').val();
                var run3000 = $('#run3000').val();
                var snakeRun = $('#snakeRun').val();
                var pullUp = $('#pullUp').val();
                var sitUp = $('#sitUp').val();

                // 验证数据
                if (!age || !height || !weight || !run3000 || !snakeRun || !pullUp || !sitUp) {
                    layer.msg('请填写完整信息！', {icon: 2});
                    return;
                }

                // 验证3000米时间格式
                if (!/^\d{1,2}:\d{2}$/.test(run3000)) {
                    layer.msg('3000米时间格式错误，请使用 MM:SS 格式', {icon: 2});
                    return;
                }

                // 显示加载中
                var loadIndex = layer.load(1, {
                    shade: [0.1, '#fff']
                });

                // 构建请求数据
                var requestData = {
                    age: parseInt(age),
                    height: parseFloat(height),
                    weight: parseFloat(weight),
                    run3000: run3000,
                    snakeRun: parseFloat(snakeRun),
                    pullUp: parseInt(pullUp),
                    sitUp: parseInt(sitUp)
                };

                // 根据性别选择API端点
                var apiUrl = gender === 'male' ? '/api/male/calculate' : '/api/female/calculate';

                // 发送请求
                $.ajax({
                    url: apiUrl,
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(requestData),
                    success: function(response) {
                        layer.close(loadIndex);
                        if (response.error) {
                            layer.msg('计算错误：' + response.error, {icon: 2});
                        } else {
                            displayResults(response);
                        }
                    },
                    error: function(xhr, status, error) {
                        layer.close(loadIndex);
                        layer.msg('请求失败，请检查网络连接或服务器状态', {icon: 2});
                        console.error('API请求错误:', error);
                    }
                });
            });

            // 重置按钮点击事件
            $('#reset-btn').on('click', function(){
                // 清空所有输入框
                $('input[type="number"], input[type="text"]').val('');
                // 隐藏结果区域
                $('#result-section').hide();
                layer.msg('已重置', {icon: 1});
            });

            // 显示计算结果
            function displayResults(data) {
                // 显示结果区域
                $('#result-section').show();

                // 更新BMI信息
                $('#bmi-value').text(data.bmi);
                $('#bmi-grade').text(data.bmiGrade);

                // 更新各项得分和等级
                $('#score-run3000').text(data.scores.run3000);
                $('#grade-run3000').text(data.grades.run3000);
                $('#score-snakeRun').text(data.scores.snakeRun);
                $('#grade-snakeRun').text(data.grades.snakeRun);
                $('#score-pullUp').text(data.scores.pullUp);
                $('#grade-pullUp').text(data.grades.pullUp);
                $('#score-sitUp').text(data.scores.sitUp);
                $('#grade-sitUp').text(data.grades.sitUp);

                // 更新总分和总等级
                $('#total-score').text(data.total);

                var totalGradeElement = $('#total-grade');
                totalGradeElement.text(data.totalGrade);

                // 根据等级设置颜色
                totalGradeElement.removeClass('grade-excellent grade-good grade-pass grade-fail');
                switch(data.totalGrade) {
                    case '优秀':
                        totalGradeElement.addClass('grade-excellent');
                        break;
                    case '良好':
                        totalGradeElement.addClass('grade-good');
                        break;
                    case '及格':
                        totalGradeElement.addClass('grade-pass');
                        break;
                    case '不及格':
                        totalGradeElement.addClass('grade-fail');
                        break;
                }

                // 各项等级颜色
                setGradeColor('run3000', data.grades.run3000);
                setGradeColor('snakeRun', data.grades.snakeRun);
                setGradeColor('pullUp', data.grades.pullUp);
                setGradeColor('sitUp', data.grades.sitUp);

                layer.msg('计算完成！', {icon: 1});
            }

            // 设置等级颜色
            function setGradeColor(project, grade) {
                var element = $('#grade-' + project);
                element.removeClass('grade-excellent grade-good grade-pass grade-fail');

                switch(grade) {
                    case '优秀':
                        element.addClass('grade-excellent');
                        break;
                    case '良好':
                        element.addClass('grade-good');
                        break;
                    case '及格':
                        element.addClass('grade-pass');
                        break;
                    case '不及格':
                        element.addClass('grade-fail');
                        break;
                }
            }

            // 表单验证 - 放宽验证条件
            $('input[type="number"]').on('blur', function(){
                var value = $(this).val();
                var min = parseFloat($(this).attr('min'));
                var max = parseFloat($(this).attr('max'));

                if (value && (value < min || value > max)) {
                    layer.msg('输入值超出范围！允许范围：' + min + ' - ' + max, {icon: 2});
                    $(this).val('');
                }
            });

            // 3000米时间格式验证
            $('#run3000').on('blur', function(){
                var value = $(this).val();
                if (value && !/^\d{1,2}:\d{2}$/.test(value)) {
                    layer.msg('请使用 MM:SS 格式，例如：12:30', {icon: 2});
                    $(this).val('');
                }
            });
        });
    </script>
</body>
</html>